#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Car.h"
#include "Parkinglot.h"
#include "SideWalk.h"

//创建一个停车场
ParkingLot* Park_Create() {
    ParkingLot* park = (ParkingLot*)malloc(sizeof(ParkingLot));
    for (int i = 0; i < MAX_CARS; i++) {
        park->spot[i] = NULL;
    }
    park->sum = 0;
    return park;
}

//判断停车场是否为空，是返回1，否则0
int Park_IsEmpty(ParkingLot* park) {
    int is = (park->sum == 0 ? 1 : 0);
    return is;
}

//判断停车场是否为满，是返回1，否则0
int Park_IsFull(ParkingLot* park) {
    int is = (park->sum == MAX_CARS ? 1 : 0);
    return is;
}



//输入车牌号搜索车位，若无则返回-1
int Park_Check(ParkingLot* park /*, char* plate*/) {
    char plate[PLATE_SIZE];
    printf("请输入车牌号\n");

    scanf("%s", plate);

    for (int i = 0; i < MAX_CARS; i++) {
        if(park->spot[i] != NULL)
        if (strcmp(park->spot[i]->license_plate ,plate) == 0) {
            printf("位于车位：%d\n", i);
            return i;
            break;
        }
    }
    printf("未找到车辆\n");
    return -1;
}

//输入车牌号，车辆进入停车场
ParkingLot* Park_Add(ParkingLot* park, Car* C, SideWalk* sidewalk) {
    if (!Park_IsFull(park)) {
        for (int i = 0; i < MAX_CARS; i++)
            if (park->spot[i] == NULL) {
                park->spot[i] = C;
                park->spot[i]->in_time = time(NULL);
                park->sum++;
                break;
            }
    }
    else {
        printf("停车场已满,");
        SideWalk_Add(sidewalk, C);
    }
    return park;

}

//扫描车牌，离开停车场
ParkingLot* Park_Leave(ParkingLot* park, ParkRecord* parkrecord) {
    char plate[PLATE_SIZE];
    printf("请输入车牌号\n");
    scanf("%s", plate);
    for (int i = 0; i < MAX_CARS; i++) {
        if (park->spot[i] != NULL) {
            if (strcmp(park->spot[i]->license_plate, plate) == 0) {

            park->spot[i]->out_time = time(NULL);
            Car_Bill(park->spot[i]);
            Park_Record(park->spot[i], parkrecord);
            park->spot[i] = NULL;
            park->sum--;
            return park;
            }
        }
    }
    printf("停车场内无该车辆");

    return park;
}

//打印目前停车场所有车辆信息
void Park_Allcheck(ParkingLot* park) {
    for (int i = 0; i < MAX_CARS; i++) {
        if (park->spot[i] != NULL)
            Car_Check(park->spot[i]);
    }
    printf("当前停车场内共有%d辆车\n", park->sum);
    printf("输出完成\n");
}

ParkRecord* Record_Create()
{
    ParkRecord* parkrecord = (ParkRecord*)malloc(sizeof(ParkRecord));
    parkrecord->k = 0;
    parkrecord->sum = 0;
    return parkrecord;
}

void Park_Record(Car* car, ParkRecord* parkrecord)
{
    parkrecord->record[(parkrecord->k)++] = car;
    parkrecord->sum++;
}

void Account_Check(ParkRecord* parkrecord)
{
    double sum = 0;
    for (int i = 0; i < parkrecord->sum; i++)
    {
        sum += parkrecord->record[i]->costs;
    }
    printf("收入:%.2f元\n", sum);
}

void Record_Check(ParkRecord* parkrecord)
{
    printf("共有%d辆车\n", parkrecord->sum);
    for (int i = 0; i < MAX_CARS; i++) {
        if (parkrecord->record[i] != NULL)
            Car_Check(parkrecord->record[i]);
    }
}